<?php
/**
 * 
 * @author tangbl
 *
 */
class IndexAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();
	}
	
	/**
	 * 列表
	 */
	public function log() {
		if(IS_AJAX){
			
			import('ORG.Util.Page');// 导入分页类
			$article      = D("Log");
			
			$orderDesc = $this->_get("orderDirection","trim","desc");;
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			
			$map       = unescape($this->_get("where"));
			$map       = "1=1 ".$map;
			$count     = $article->where($map)->count();
			$Page      = new Page($count);
			$nowPage   = isset($_GET['p'])?$_GET['p']:1;
			$show      = $Page->show();
			$list      = $article->where($map)->order($orderName.' '.$orderDesc)
						->page($nowPage.','.$pageNum)->select();
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
			
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			foreach($list AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
					'id'       => $row['id'],
					'module'   => $row['module'],
					'content'  => $row['content'],
					'addtime'  => date('Y-m-d H:i',$row['addtime']),
					'deal'=>'<a href="#" class="btn red icn-only delAlone" title="'.L('cfg_list_itf_btn_delete').
					'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a> '
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
			
		}else{
			$model = M("User");
			$list  = $model->field("id,username,realname")->select();
			$this->assign("users",$list);
			$this->display();
		}
		
	}
	
	/**
	 * 删除日志
	 */
	public function dellog() {
		$id = $this->_param('id','intval','0');//获取id值
		if(!$id) $this->error(L('cfg_form_tipmsg_illgel'));
		
		$model   = M("Log");
		$where   = "id in ($id)";
		$res     = $model->where($where)->delete();

		if($res) {
			$this->ajaxReturn($res,L('cfg_form_tipmsg_delsucc'),1);
		} else {
			$this->ajaxReturn(0,L('cfg_form_tipmsg_delfail'),0);
		}
	}
	
	/**
	 * 后台首页 查看系统信息
	 */
    public function index(){
		$user = M('User');
		$user_data  = $user->find(session('userid')); 
		
		$user_pic = find_thumb($user_data['solevar'],'single');
		if($user_pic['fileurl']==""){
			$user_pic['fileurl'] = 'System/user_pic.jpg';
		}
		
		$role = M('role');
		$role_data  = $role->find($user_data['role']); 
		
		$user_data['sex']  = $user_data['sex']==1?L('idx_form_ele_sex_1'):L('idx_form_ele_sex_2');
		$user_data['role'] = $role_data['name'];
		$user_data['last_login_time'] = date('Y-m-d H:i',$user_data['last_login_time']);
		
		$this->assign("user",$user_data);
		$this->assign("user_pic",$user_pic);
		
		$member = D("Member");
		$mcount = $member->getMemberCount();
		$rcount = $member->getReplyLogCount();
		$counts = array("member"=>$mcount,"reply"=>$rcount);
		$this->assign("count",$counts);
		
		$this->display();
    }
	
	/**
	 * 清除全部缓存
	 */
	public function cleancache(){
		
		//重新生成权限菜单缓存
		$access = D("Access");
		$role   = D("Role");
		$roles  = $role->getAllRole();
		foreach($roles as $v){
			$access->clearRoleCache($v['id']);
		}
		
		//重新生成栏目缓存
		$menu    = D("Category");
		$arr_lan = C('LANG_ARRAY');
		$arr_men = C('LANG_CATEGORY');
		foreach($arr_lan as $k=>$v){
			F("cate_".$k,NULL);
			$menu->getCateByModule($k);
			foreach($arr_men as $a=>$b){
				F("cate_".$k.$a,NULL);
				$menu->getCateByModule($k,$a);
			}
		}
		
		//删除列表缓存目录
		foreach($arr_men as $k=>$v){
			$path = DATA_PATH."/".$k;
			if(is_dir($path)){
				list_del_dirfile($path);
			}
		}
		$del_arr = array("Resource","Integral");//,"Block"
		foreach($del_arr as $k){
			$path = DATA_PATH."/".$k;
			if(is_dir($path)){
				list_del_dirfile($path);//echo "|dir:".$path;
			}
		}
		
		//生成模型缓存
		D("Module")->cacheModule();
		
		//生成碎片缓存
		$model = D("Block");
		$list  = $model->select();
		foreach($list as $k=>$v){
			$key   = $v['key'];
			$value = json_decode($v['value'],true);
			F("$key",$value,DATA_PATH."Block/");
		}
		
		//删除行为十天前的行为数据
		$map['addtime'] = array("LT",time()-86400*10);
		$model  = M("Behavior");
		$model->where($map)->delete();
		$_table = C('DB_PREFIX')."behavior";
		$sql = "OPTIMIZE TABLE  `".$_table."`";
		$model->query($sql);
		//echo date("Y-m-d H:i:s",1389270057);echo M("Behavior")->getLastSql();exit();
		
		//重新生成字典缓存
		$dict = D("Dict");
		$dict->cacheDict();
		
		wlog(1);//记录日志
		$this->assign("jumpUrl",U('/Index/index'));   
		$this->success(L('cfg_form_cleancache_succ'));
	}
	
	/**
	 * 系统设置
	 */
	public function config(){
		$sysconfig = D("Sysconfig");
		$lists     = $sysconfig->select();
		if(IS_POST){
			$cfg =  $this->_post("cfg");
			
			$image = array("wxpic","wxerweima","wxbigpic","wxsmallpic");
			foreach($image as $v){
				$url = implode($this->_post($v."_url","",""));
				$row['title'] = implode($this->_post($v."_title","",""));
				$row['url']   = substr_replace($url,"",0,strlen(UPLOAD_URL));
				$cfg[$v] = $row;
			}
			
			F("Sysconfig",$cfg);
			wlog(1);
			$this->assign("jumpUrl",U('/Index/config')); 
			$this->success(L("idx_form_cfg_success"));
		}else{
			$langArray = C('LANG_ARRAY');
			$this->assign("langList",$langArray);
			
			$this->assign("lists",F("Sysconfig"));
			$this->display();
		}
	}
}

function list_del_dirfile($dir,$delDir=''){
	$list = scandir($dir); 
  	foreach($list as $file){
		$path    = $dir."/".$file;
		//$pattern = "/^\.svn/";&& !preg_match($pattern,$file)
		if( $file!="." && $file!=".." ){
			if( is_dir($path) ){//&& in_array($file,$delDir)
			   list_del_dirfile($path,$delDir);
			}else{
				if(is_file($path))
				  //echo "file:".$path."<br>"; 
				  @unlink($path);
			}
		}
	}	
}